//
//  ProfileViewController.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/24.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

class ProfileViewController: UIViewController {

    //MARK: - Properties
    lazy var tableView: UITableView = {
        let tableView = UITableView.adjustedTableView(viewController: self)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.allowsSelection = false
        tableView.separatorStyle = .none
        tableView.registerCellNib(with: ProfileUserCell.self)
        tableView.registerCellNib(with: ProfileQuestionsCell.self)
        tableView.registerCellClass(with: ProfileOrderCell.self)
        tableView.registerCellClass(with: ProfileProjectCell.self)
//        if @available(iOS 11.0, *) {
//            tableView.contentInsetAdjustmentBehavior = .never
//        } else {
//            self.automaticallyAdjustsScrollViewInsets = false
//        }
        return tableView
    }()

    lazy var messageItem: UIBarButtonItem = {
        let item = UIBarButtonItem(image: #imageLiteral(resourceName: "profile_nav_message").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(messageButtonClicked))
        return item
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.rightBarButtonItem = messageItem
        navigationItem.title = "我"
        navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont.systemFont(ofSize: 20)]
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.barTintColor = UIColor(hexString: "#2C2C2C")

        view.addSubview(tableView)
        tableView.mas_makeConstraints { (make) in
            make?.edges.equalTo()(view)
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        tableView.reloadData()
    }

    func generateTexts() {
        var texts: [String] = []
        for _ in 0...(5 + arc4random_uniform(10)) {
            var str = ""
            for _ in 0...(2 + arc4random_uniform(10)) {
                str += "问题"
            }
            texts.append(str)
        }
        self.texts = texts
    }

    var texts: [String] = {
        var texts: [String] = []
        for _ in 0...(5 + arc4random_uniform(10)) {
            var str = ""
            for _ in 0...(2 + arc4random_uniform(10)) {
                str += "问题"
            }
            texts.append(str)
        }
        return texts
    }()
}

private extension ProfileViewController {
    @objc func messageButtonClicked() {
        let vc = AvatarViewController()
        vc.modalPresentationStyle = .overCurrentContext
        vc.modalTransitionStyle = .crossDissolve
        self.tabBarController?.present(vc, animated: true, completion: nil)
    }
}


extension ProfileViewController: UITableViewDataSource, UITableViewDelegate {

    enum Row: Int {
        case user
        case order
        case project
        case question
        var rowHeight: CGFloat {
            switch self {
            case .user: return 300
            case .question: return UITableViewAutomaticDimension
            default: return 200

            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let row = Row(rawValue: indexPath.row) else { fatalError() }
        switch row {
        case .user:
            let cell: ProfileUserCell = tableView.dequeueReusableCell(for: indexPath)
            cell.addTagAction = { [weak self] in
                guard let me = self else { return }
                let vc = TagsEditViewController()
                vc.present(by: me.tabBarController!)
            }
            return cell

        case .order:
            let cell: ProfileOrderCell = tableView.dequeueReusableCell(for: indexPath)


            return cell
        case .project:
            let cell: ProfileProjectCell = tableView.dequeueReusableCell(for: indexPath)

            return cell
        case .question:
            let cell: ProfileQuestionsCell = tableView.dequeueReusableCell(for: indexPath)
            cell.questionsListView.maxWidth = view.width - 60
            cell.questionsListView.texts = texts
            cell.reloadQuestionAction = { [weak self] (index) in
                guard let me = self else { return }
                me.generateTexts()
                me.tableView.reloadData()
            }
            cell.searchView.rightButtonTapsAction = { [weak self] in
                guard let me = self else { return }
//                let vc = QuestionsViewController()//
                let vc = AddOrderViewController()//AddOrderViewController()//MyRepairViewController()
                vc.present(by: me)
//              self?.navigationController?.pushViewController(vc, animated: true)
            }
            return cell
        }
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        guard let row = Row(rawValue: indexPath.row) else { fatalError() }
        return row.rowHeight
    }
}
